package com.xinke.edu.mypetadoption.Activity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xinke.edu.mypetadoption.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 结算界面适配器
 */
public class CheckoutAdapter extends RecyclerView.Adapter<CheckoutAdapter.CheckoutViewHolder> {
    private List<ShoppingCartAdapter.CartItem> cartItems = new ArrayList<>();
    private static final DecimalFormat df = new DecimalFormat("0.00");

    public CheckoutAdapter() {
    }

    // 设置购物车数据
    public void setCartItems(List<ShoppingCartAdapter.CartItem> cartItems) {
        this.cartItems = cartItems != null ? cartItems : new ArrayList<>();
        notifyDataSetChanged();
    }

    @Override
    public CheckoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checkout, parent, false);
        return new CheckoutViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CheckoutViewHolder holder, int position) {
        if (cartItems != null && position < cartItems.size()) {
            ShoppingCartAdapter.CartItem item = cartItems.get(position);
            
            // 绑定数据
            holder.productName.setText(item.getName());
            holder.productPrice.setText("¥" + df.format(item.getPrice()));
            holder.productQuantity.setText("x" + item.getQuantity());
            
            // 计算小计
            double subtotal = item.getPrice() * item.getQuantity();
            holder.productSubtotal.setText("¥" + df.format(subtotal));
            
            // 加载商品图片
            if (item.getImageUrl() != null && !item.getImageUrl().isEmpty()) {
                Glide.with(holder.itemView.getContext())
                        .load(item.getImageUrl())
                        .placeholder(R.drawable.ic_launcher_background) // 占位图
                        .error(R.drawable.ic_launcher_background) // 错误图
                        .into(holder.productImage);
            }
        }
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    static class CheckoutViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productName;
        TextView productPrice;
        TextView productQuantity;
        TextView productSubtotal;

        public CheckoutViewHolder(@NonNull View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.checkout_product_image);
            productName = itemView.findViewById(R.id.checkout_product_name);
            productPrice = itemView.findViewById(R.id.checkout_product_price);
            productQuantity = itemView.findViewById(R.id.checkout_product_quantity);
            productSubtotal = itemView.findViewById(R.id.checkout_product_subtotal);
        }
    }
}